using System;
using System.IO;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.NotificationServices
{
	internal class Notification
	{
		protected internal static sbyte[] eventmessageline;

		protected internal static sbyte[] poststring;

		protected internal static sbyte[] headerseparator;

		protected internal static sbyte[] crlf;

		protected internal static sbyte[] versionheader;

		protected internal static sbyte[] versionheaderid;

		protected internal static sbyte[] typeheader;

		protected internal static sbyte[] affectedcomponentsheader;

		protected internal static sbyte[] affectednodesheader;

		protected internal static sbyte[] generatingcomponentheader;

		protected internal static sbyte[] generatingnodeheader;

		protected internal static sbyte[] generatingprocessheader;

		protected internal static sbyte[] idheader;

		protected internal static sbyte[] clusteridheader;

		protected internal static sbyte[] clusternameheader;

		protected internal static sbyte[] instanceidheader;

		protected internal static sbyte[] instancenameheader;

		protected internal static sbyte[] creationtimeheader;

		protected internal static sbyte[] contentlengthheader;

		protected internal static sbyte[] numpropertiesheader;

		protected internal static sbyte[] localonlyheader;

		protected internal static sbyte[] stampheader;

		protected internal static sbyte[] hostname;

		protected internal static sbyte[] truestring;

		protected internal static sbyte[] falsestring;

		protected internal static sbyte[] versionheaderid3;

		protected internal static sbyte[] clusteronlyheader;

		private string type_Renamed_Field;

		private string affectedComponents_Renamed_Field;

		private string affectedNodes_Renamed_Field;

		private sbyte[] body_Renamed_Field;

		protected internal bool localonly;

		protected internal long deliveryTime_Renamed_Field;

		protected internal string generatingComponent_Renamed_Field;

		protected internal string generatingNode_Renamed_Field;

		protected internal string generatingProcess_Renamed_Field;

		protected internal string id_Renamed_Field;

		protected internal long creationTime_Renamed_Field;

		protected internal string clusterId_Renamed_Field;

		protected internal string clusterName_Renamed_Field;

		protected internal string instanceId_Renamed_Field;

		protected internal string instanceName_Renamed_Field;

		protected internal ONS oems;

		private PropertyList properties;

		protected internal int[] recipients;

		protected internal Notification(InputBuffer ibuf, ONS oems)
		{
			int num = 0;
			int num2 = 0;
			bool flag = false;
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.Notification, OracleTraceFuncName.ctor);
			}
			try
			{
				string nextString = ibuf.NextString;
				if (nextString[0] == 'o')
				{
					flag = true;
					nextString = ibuf.NextString;
					nextString = ibuf.NextString;
				}
				ibuf.skipBytes(typeheader.Length);
				type_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(affectedcomponentsheader.Length);
				affectedComponents_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(affectednodesheader.Length);
				affectedNodes_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(generatingcomponentheader.Length);
				generatingComponent_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(generatingprocessheader.Length);
				generatingProcess_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(generatingnodeheader.Length);
				generatingNode_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(idheader.Length);
				id_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(creationtimeheader.Length);
				nextString = ibuf.NextString;
				if (nextString != null)
				{
					try
					{
						creationTime_Renamed_Field = long.Parse(nextString);
					}
					catch (FormatException ex)
					{
						if (ProviderConfig.m_bTraceLevelPrivate)
						{
							Trace.Write(OracleTraceLevel.Private, OracleTraceTag.Error, OracleTraceClassName.Notification, OracleTraceFuncName.ctor, "Notification::Notification(InputBuffer, ONS) failed. -" + ex.Message);
						}
						throw new IOException();
					}
				}
				else
				{
					creationTime_Renamed_Field = -1L;
				}
				ibuf.skipBytes(clusteridheader.Length);
				clusterId_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(clusternameheader.Length);
				clusterName_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(instanceidheader.Length);
				instanceId_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(instancenameheader.Length);
				instanceName_Renamed_Field = ibuf.NextString;
				ibuf.skipBytes(localonlyheader.Length);
				nextString = ibuf.NextString;
				if (nextString != null)
				{
					if (string.Equals(nextString, "true", StringComparison.InvariantCultureIgnoreCase))
					{
						localonly = true;
					}
					else
					{
						localonly = false;
					}
				}
				else
				{
					localonly = false;
				}
				if (!flag)
				{
					_ = ibuf.NextString;
				}
				ibuf.skipBytes(numpropertiesheader.Length);
				nextString = ibuf.NextString;
				if (nextString != null)
				{
					try
					{
						num2 = int.Parse(nextString);
					}
					catch (FormatException ex2)
					{
						if (ProviderConfig.m_bTraceLevelPrivate)
						{
							Trace.Write(OracleTraceLevel.Private, OracleTraceTag.Error, OracleTraceClassName.Notification, OracleTraceFuncName.ctor, "Notification::Notification(InputBuffer, ONS) failed. -" + ex2.Message);
						}
						throw new IOException();
					}
				}
				else
				{
					num2 = 0;
				}
				if (num2 > 0)
				{
					properties = new PropertyList(num2, ibuf);
				}
				else
				{
					properties = null;
				}
				ibuf.skipBytes(contentlengthheader.Length);
				nextString = ibuf.NextString;
				if (nextString != null)
				{
					try
					{
						num = int.Parse(nextString);
					}
					catch (FormatException ex3)
					{
						OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.Notification, OracleTraceFuncName.ctor, ex3);
						throw new IOException();
					}
				}
				else
				{
					num = 0;
				}
				ibuf.skipBytes(14);
				nextString = ibuf.NextString;
				if (nextString != null)
				{
					int num3 = 1;
					int startIndex = 0;
					int num4;
					while ((num4 = nextString.IndexOf(';', startIndex)) != -1)
					{
						num3++;
						startIndex = num4 + 1;
					}
					recipients = new int[num3];
					startIndex = 0;
					for (int i = 0; i < num3; i++)
					{
						num4 = nextString.IndexOf(';', startIndex);
						string s = ((num4 != -1) ? nextString.Substring(startIndex, num4 - startIndex) : nextString.Substring(startIndex));
						try
						{
							recipients[i] = int.Parse(s);
						}
						catch (FormatException ex4)
						{
							OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.Notification, OracleTraceFuncName.ctor, ex4);
							recipients[i] = -1;
						}
						startIndex = num4 + 1;
					}
				}
				else
				{
					recipients = null;
				}
				ibuf.skipBytes(2);
				if (num > 0)
				{
					body_Renamed_Field = new sbyte[num];
					ibuf.getBytes(body_Renamed_Field, num);
				}
				else
				{
					body_Renamed_Field = null;
				}
				deliveryTime_Renamed_Field = (DateTime.Now.Ticks - 621355968000000000L) / 10000;
			}
			catch (Exception ex5)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.Notification, OracleTraceFuncName.ctor, ex5);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.Notification, OracleTraceFuncName.ctor);
				}
			}
		}

		protected internal virtual void send(OutputBuffer obuf, ONS oems, Connection connection)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.Notification, OracleTraceFuncName.send);
			}
			try
			{
				obuf.putBytes(eventmessageline, eventmessageline.Length);
				obuf.putBytes(versionheader, versionheader.Length);
				if (connection.ServerVersion == 3)
				{
					obuf.putBytes(versionheaderid3, versionheaderid.Length);
				}
				else
				{
					obuf.putBytes(versionheaderid, versionheaderid.Length);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(typeheader, typeheader.Length);
				if (type_Renamed_Field != null)
				{
					obuf.putString(type_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(affectedcomponentsheader, affectedcomponentsheader.Length);
				if (affectedComponents_Renamed_Field != null)
				{
					obuf.putString(affectedComponents_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(affectednodesheader, affectednodesheader.Length);
				if (affectedNodes_Renamed_Field != null)
				{
					obuf.putString(affectedNodes_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(generatingcomponentheader, generatingcomponentheader.Length);
				if (generatingComponent_Renamed_Field != null)
				{
					obuf.putString(generatingComponent_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(generatingprocessheader, generatingprocessheader.Length);
				if (generatingProcess_Renamed_Field != null)
				{
					obuf.putString(generatingProcess_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(generatingnodeheader, generatingnodeheader.Length);
				if (generatingNode_Renamed_Field != null)
				{
					obuf.putString(generatingNode_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(idheader, idheader.Length);
				if (id_Renamed_Field != null)
				{
					obuf.putString(id_Renamed_Field);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(creationtimeheader, creationtimeheader.Length);
				long num = creationTime_Renamed_Field;
				sbyte[] array = SupportClass.ToSByteArray(SupportClass.ToByteArray(num.ToString()));
				obuf.putBytes(array, array.Length);
				obuf.putBytes(crlf, 2);
				obuf.putBytes(clusteridheader, clusteridheader.Length);
				if (oems.clusterid != null)
				{
					obuf.putString(oems.clusterid);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(clusternameheader, clusternameheader.Length);
				if (oems.clustername != null)
				{
					obuf.putString(oems.clustername);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(instanceidheader, instanceidheader.Length);
				if (oems.instanceid != null)
				{
					obuf.putString(oems.instanceid);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(instancenameheader, instancenameheader.Length);
				if (oems.instancename != null)
				{
					obuf.putString(oems.instancename);
				}
				obuf.putBytes(crlf, 2);
				obuf.putBytes(localonlyheader, localonlyheader.Length);
				if (localonly)
				{
					obuf.putBytes(truestring, truestring.Length);
				}
				else
				{
					obuf.putBytes(falsestring, falsestring.Length);
				}
				obuf.putBytes(crlf, 2);
				if (connection.ServerVersion == 3)
				{
					obuf.putBytes(clusteronlyheader, clusteronlyheader.Length);
					obuf.putBytes(falsestring, falsestring.Length);
					obuf.putBytes(crlf, 2);
				}
				obuf.putBytes(numpropertiesheader, numpropertiesheader.Length);
				if (properties != null)
				{
					array = SupportClass.ToSByteArray(SupportClass.ToByteArray(properties.num().ToString()));
					obuf.putBytes(array, array.Length);
					obuf.putBytes(crlf, 2);
					properties.write(obuf);
				}
				else
				{
					obuf.putByte(48);
					obuf.putBytes(crlf, 2);
				}
				if (body_Renamed_Field != null)
				{
					obuf.putBytes(contentlengthheader, contentlengthheader.Length);
					array = SupportClass.ToSByteArray(SupportClass.ToByteArray(body_Renamed_Field.Length.ToString()));
					obuf.putBytes(array, array.Length);
					obuf.putBytes(crlf, 2);
				}
				else
				{
					obuf.putBytes(contentlengthheader, contentlengthheader.Length);
					obuf.putByte(48);
					obuf.putBytes(crlf, 2);
				}
				obuf.putBytes(crlf, 2);
				if (body_Renamed_Field != null)
				{
					obuf.putBytes(body_Renamed_Field, body_Renamed_Field.Length);
				}
				obuf.flush();
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.Notification, OracleTraceFuncName.send, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.Notification, OracleTraceFuncName.send);
				}
			}
		}

		public virtual string type()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.Notification, OracleTraceFuncName.type);
			}
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.Notification, OracleTraceFuncName.type);
			}
			return type_Renamed_Field;
		}

		public virtual sbyte[] body()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.Notification, OracleTraceFuncName.body);
			}
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.Notification, OracleTraceFuncName.body);
			}
			return body_Renamed_Field;
		}

		static Notification()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097408, OracleTraceClassName.Notification, OracleTraceFuncName.cctor);
			}
			try
			{
				crlf = new sbyte[2];
				crlf[0] = 13;
				crlf[1] = 10;
				eventmessageline = new sbyte[22];
				Array.Copy(SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("POST /event HTTP/1.1").ToString())), 0, eventmessageline, 0, 20);
				Array.Copy(crlf, 0, eventmessageline, 20, 2);
				poststring = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("POST ").ToString()));
				headerseparator = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder(": ").ToString()));
				versionheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("Version: ").ToString()));
				versionheaderid = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder(Convert.ToString(4)).ToString()));
				typeheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("eventType: ").ToString()));
				affectedcomponentsheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("affectedComponents: ").ToString()));
				affectednodesheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("affectedNodes: ").ToString()));
				generatingcomponentheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("generatingComponent: ").ToString()));
				generatingnodeheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("generatingNode: ").ToString()));
				generatingprocessheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("generatingProcess: ").ToString()));
				idheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("eventId: ").ToString()));
				clusteridheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("clusterId: ").ToString()));
				clusternameheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("clusterName: ").ToString()));
				instanceidheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("instanceId: ").ToString()));
				instancenameheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("instanceName: ").ToString()));
				creationtimeheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("creationTime: ").ToString()));
				contentlengthheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("Content-Length: ").ToString()));
				numpropertiesheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("numberOfProperties: ").ToString()));
				localonlyheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("LocalOnly: ").ToString()));
				stampheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("stamp: ").ToString()));
				hostname = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("hostName: ").ToString()));
				truestring = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("true").ToString()));
				falsestring = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("false").ToString()));
				versionheaderid3 = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("3").ToString()));
				clusteronlyheader = SupportClass.ToSByteArray(SupportClass.ToByteArray(new StringBuilder("ClusterOnly: ").ToString()));
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)270532608, OracleTraceClassName.Notification, OracleTraceFuncName.cctor, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)2097664, OracleTraceClassName.Notification, OracleTraceFuncName.cctor);
				}
			}
		}
	}
}
